<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Sql.class.php";
require_once dirname(__FILE__)."/StatusRoster.class.php";
require_once dirname(__FILE__)."/Revision.class.php";
require_once dirname(__FILE__)."/Localization.class.php";

class RevisionManager
{
	static private $instance;
	
	private $f_delete_db;
	private $f_install_db;
	private $sql;
	private $status_roster;
	private $localization;
	private $revision;
	
	private function __construct()
	{
		$this->sql = Sql::GetInstance();
		$this->status_roster = StatusRoster::GetInstance();
		$this->localization = Localization::GetInstance();
		$this->revision = Revision::GetInstance();
		
		$this->f_delete_db = file( dirname(__FILE__)."/../class.data/RevisionManager.f_delete_db.serialize" );
		if ($this->f_delete_db == FALSE)
		{
			trigger_error("Could not open/read class.data/RevisionManager.f_delete_db.serialize.", E_USER_ERROR);
		}
		
		$this->f_install_db = file( dirname(__FILE__)."/../class.data/RevisionManager.f_install_db.serialize" );
		if ($this->f_install_db == FALSE)
		{
			trigger_error("Could not open/read class.data/RevisionManager.f_install_db.serialize.", E_USER_ERROR);
		}
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new RevisionManager();
		}
		
		return self::$instance;
	}
	
	function DeleteDb()
	{
		foreach($this->f_delete_db as $line)
		{
			if ( $this->sql->Query($line) != true )
			{
				return false;
			}
		}
		
		return true;
	}
	
	function CreateDb()
	{
		$return_value = true;
		
		foreach($this->f_install_db as $line)
		{
			if ($this->sql->Query($line) != true)
			{
				if ($return_value == true)
				{
					$return_value = false;
				}
				
				$this->status_roster->AddError($this->localization->Get('could not create the table %s'), $this->sql->GetLastError());
			}
		}
		
		if ($this->revision->UpdateInstalledRevision() == false)
		{
			$this->status_roster->AddError($this->localization->Get('could not update the installed revision number'));
			
			return false;
		}
		
		return $return_value;
	}
}

?>